#include "LT89xxdrvSPI.H"
#include "N76E003.h"
unsigned char RegH,RegL;


void DelayMs(unsigned char Tms)		//unit:1mS
{
	unsigned int dly;
	while(Tms)
	{
		Tms--;
		dly=1000;
		while(dly)	dly--;
	}
}

//----------------------------------------------------------


void SPI_SendByte(unsigned char buf)
{
#ifndef FastSPI
	unsigned char mcnt;
	for(mcnt=0;mcnt<8;mcnt++)
	{
	RF_CLK=0; 	//Bus ready;

	if(buf&0x80)   	RF_MOSI=1;
				else  	 	RF_MOSI=0;

	RF_CLK=1;	//Send bit;	

	buf<<=1;

	}
	RF_CLK=0; 	//Bus ready;
	RF_MOSI=0;	
	
	
	
#else
		RF_CLK=1;
		if(buf&0x80)	RF_MOSI=1;
		else			RF_MOSI=0;
		RF_CLK=0;

		RF_CLK=1;
		if(buf&0x40)	RF_MOSI=1;
		else			RF_MOSI=0;
		RF_CLK=0;

		RF_CLK=1;
		if(buf&0x20)	RF_MOSI=1;
		else			RF_MOSI=0;
		RF_CLK=0;

		RF_CLK=1;
		if(buf&0x10)	RF_MOSI=1;
		else			RF_MOSI=0;
		RF_CLK=0;

		RF_CLK=1;
		if(buf&0x08)	RF_MOSI=1;
		else			RF_MOSI=0;
		RF_CLK=0;

		RF_CLK=1;
		if(buf&0x04)	RF_MOSI=1;
		else			RF_MOSI=0;
		RF_CLK=0;

		RF_CLK=1;
		if(buf&0x02)	RF_MOSI=1;
		else			RF_MOSI=0;
		RF_CLK=0;

		RF_CLK=1;
		if(buf&0x01)	RF_MOSI=1;
		else			RF_MOSI=0;
		RF_CLK=0;
#endif	
}


unsigned char SPI_ReadByte(void)
{
	unsigned char retvalue=0;
#ifndef FastSPI
	unsigned char mcnt;
	RF_CLK=0;		//Bus ready
	for (mcnt=0;mcnt<8;mcnt++)
	{
 	RF_CLK=1;	//Slaver Chip OutPut Data

	retvalue<<=1;
	RF_CLK=0;		//Bus ready	
 	
	
	if(RF_MISO)	retvalue|=1;
	else		retvalue&=0xfe;

	}
	RF_CLK=0;		//Bus ready	
#else
	
	RF_CLK=1;
	RF_CLK=0;
	if(RF_MISO)	retvalue|=0X80;

	RF_CLK=1;
	RF_CLK=0;
	if(RF_MISO)	retvalue|=0X40;

	RF_CLK=1;
	RF_CLK=0;
	if(RF_MISO)	retvalue|=0X20;

	RF_CLK=1;
	RF_CLK=0;
	if(RF_MISO)	retvalue|=0X10;

	RF_CLK=1;
	RF_CLK=0;
	if(RF_MISO)	retvalue|=0X08;

	RF_CLK=1;
	RF_CLK=0;
	if(RF_MISO)	retvalue|=0X04;

	RF_CLK=1;
	RF_CLK=0;
	if(RF_MISO)	retvalue|=0X02;

	RF_CLK=1;
	RF_CLK=0;
	if(RF_MISO)	retvalue|=0X01;
#endif	
	return retvalue;
}




void LT_ReadReg(unsigned char reg)
{
	RF_SS=0;
	SPI_SendByte(reg|0x80);	//Read MSB.7=1;	
	RegH=SPI_ReadByte(); 
	RegL=SPI_ReadByte(); 
	RF_SS=1;

}

void LT_WriteReg(unsigned char reg, unsigned char H, unsigned char L)
{
	RF_SS=0;
	SPI_SendByte(reg); 	//Read MSB.7=0;
	SPI_SendByte(H);
	SPI_SendByte(L);
	RF_SS=1;
}



void LT_WriteBUF(unsigned char reg, unsigned char *pBuf, unsigned char len)
{
	unsigned char i;

	RF_SS=0;
	SPI_SendByte(reg); 	//Read MSB.7=0;
	SPI_SendByte(len);	//Lenth
	for(i=0; i<len; i++)
		SPI_SendByte(pBuf[i]);


	RF_SS=1;
}

unsigned char LT_ReadBUF(unsigned char reg, unsigned char *pBuf)
{
	unsigned char i,len;

	RF_SS=0;
	SPI_SendByte(reg|0X80); 	//Read MSB.7=0;
	len=SPI_ReadByte();
	for(i=0; i<len; i++)
	{
		pBuf[i] = SPI_ReadByte();
	}

	RF_SS=1;
	
	return len;
}

#define CFG_LEN  29//34//+4
code unsigned char LDT8900config[CFG_LEN][3]= //32?
{
#if 0
	{0, 0x6F, 0xE0},
	{1, 0x56, 0x81},
	{2, 0x66, 0x17},
	{4, 0x9C, 0xC9},
	{5, 0x66, 0x37},
	{7, 0x00, 0x30},	//channel freq and Start/Stop Tx,Rx
	{8, 0x6C, 0x90},	
	{9, 0x48, 0x00},	//0x4800(max:6dBm)

	{10, 0x7F, 0xFD},
	{11, 0x00, 0x08},	//RSSI on
	{12, 0x00, 0x00},
	{13, 0x48, 0xBD},

	{22, 0x00, 0xff},
	{23, 0x80, 0x05},
	{24, 0x00, 0x67},
	{25, 0x16, 0x59},
	{26, 0x19, 0xE0},
	{27, 0x13, 0x00},
	{28, 0x18, 0x00},
	
	{32, 0x48, 0x00},
	{33, 0x3f, 0xC7},
	{34, 0x20, 0x00},
	{35, 0x03, 0x00},//auto_times=3	Scs=1 Miso=Hiz
	
	{36, 0X34, 0X12},
	{37, 0x06, 0x8C},
	{38, 0x5A, 0x5A},
	{39, 0x78, 0X56},

	{40, 0x44, 0x02},
	{41, 0xB0, 0x00}, 
	{42, 0xFD, 0xB0},
	{43, 0x00, 0x0F},
	{44, 0x10, 0x00},
	{45, 0x05, 0x52},	
	{52, 0x80, 0x80},
#else
	{0, 0X6F, 0XE0},
	{1, 0X56, 0X81},
	{2, 0x66, 0X17},
	{4, 0X9C, 0XC9},
	{5, 0X66, 0X37},
	{7, 0X00, 0X4C},    ////
	{8, 0X6C, 0X90},
	{9, 0X48, 0X00},
	
	{10, 0x7f, 0xfd},
	{11, 0x00, 0x08},
	{12, 0x00, 0x00},
	{13, 0x48, 0xbd},
	
	{22, 0x00, 0xff},
	{23, 0x80, 0x05},
	{24, 0x00, 0x67},
	{25, 0x16, 0x59},
	{26, 0x19, 0xe0},
	{27, 0x13, 0xc0},  ////
	{28, 0x18, 0x00},
	
	{32, 0x48, 0x00},
	{33, 0x3f, 0xc7},
	{34, 0x20, 0x00},
	{35, 0x03, 0x80},   ////
	
	{36, 0x14, 0x7a},    //
	{39, 0x25, 0x8b},
	
	{40, 0x44, 0x02},
	{41, 0xb0, 0x00},
	{42, 0xfd, 0xb0},
	{43, 0x00, 0x0f},
#endif

};

void LT89xx_INIT(void)
{
	unsigned char CFG_CNT;
	while(1)
	{

		RF_RST = 0; 	  DelayMs(10);
		RF_RST = 1; 		DelayMs(100);

		RF_CLK=0;
		for(CFG_CNT=0;CFG_CNT<CFG_LEN;CFG_CNT++)
		{
			LT_WriteReg(LDT8900config[CFG_CNT][0],LDT8900config[CFG_CNT][1],LDT8900config[CFG_CNT][2]);

		}

		LT_ReadReg(0);
		if(RegH==0x6F&&RegL==0XE0)	
			break;
	}
}

BitStatus ReadReg_Init(void)
{	
 	BitStatus     Init_err=RESET;
	
	LT_ReadReg(0);
	if ((RegH!=0x6f)||(RegL!=0xE0))
	{
		Init_err=SET;
	}
	LT_ReadReg(1);
	if ((RegH!=0x56)||(RegL!=0x81))
	{
		Init_err=SET;
	}
	LT_ReadReg(2);
	if ((RegH!=0x66)||(RegL!=0x17))
	{
		Init_err=SET;
	}
	LT_ReadReg(4);
	if ((RegH!=0x9C)||(RegL!=0xC9))
	{
		Init_err=SET;
	}
	LT_ReadReg(5);
	if ((RegH!=0x66)||(RegL!=0x37))
	{
		Init_err=SET;
	}

	LT_ReadReg(36);
	if ((RegH!=0x42)||(RegL!=0x31))
	{
		Init_err=SET;
	}
	LT_ReadReg(37);
	if ((RegH!=0x86)||(RegL!=0x75))
	{
		Init_err=SET;
	}
	LT_ReadReg(38);
	if ((RegH!=0x9a)||(RegL!=0x0b))
	{
		Init_err=SET;
	}
	LT_ReadReg(39);
	if ((RegH!=0xde)||(RegL!=0xcf))
	{
		Init_err=SET;
	}
	
    return  Init_err;
}

unsigned char IsTxOrRxComplete(void)
{
	RegL = 0;
	LT_ReadReg(48);

	if(RegL&0x40) return 1;
	else return 0;
}

void PL1167_REC(u8 *BF)
{
	unsigned char  CK,II;

	LT_ReadReg(48);	
	RF_SS=0;
	if((!(RegH&0X80))&&(!(RegH&0X40)))
	{	
		SPI_SendByte(50+0x80);
		//DelayMs(1);//Delay_us(1);
		CK=SPI_ReadByte();//CK=ReadSpi();
		if(CK==8)
		{
			for(II=0;II<CK;II++)
			{
				BF[II]=SPI_ReadByte();
			}
		}
	}
	RF_SS=1;
	LT_WriteReg(7, 0x00,0x30);	
}

void RXD_Init(u8 Ch_NO)
{
	LT_WriteReg(7,0,0);
	LT_WriteReg(52,0x80,0x80);
	LT_WriteReg(7,0X00,0x80+Ch_NO);
}
